package br.com.amadeus.android.service;

import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.util.ArrayList;

import org.apache.http.client.ClientProtocolException;
import org.json.JSONException;

import br.com.amadeus.android.activities.home.R;
import br.com.amadeus.android.business.FacadeAmadeusDroid;
import br.com.amadeus.android.entities.Config;
import br.com.amadeus.android.entities.Course;
import br.com.amadeus.android.entities.CourseUser;
import br.com.amadeus.android.entities.SocialHistory;
import br.com.amadeus.android.util.ConstantsAmadeusDroid;
import br.com.amadeus.android.util.JSONReader;
import br.com.amadeus.android.util.RESTFileContent;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Binder;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;

public class ServiceSyncronizeAmadeus extends Service implements Runnable{
	protected static final String CATEGORIA = "curso";
			
	@Override
	public IBinder onBind(Intent arg0) {
		return null;
	}
	
	@Override
	public void onCreate() {
		new Thread(this).start();
	}
	
	@Override
	public void onStart(Intent intent, int startId) {
		super.onStart(intent, startId);
	}
	
	@Override
	public void onDestroy() {
		
	}
	
	public void run() {
		
		Config config = FacadeAmadeusDroid.getInstance(this).getConfig();
		String url = config.getAmadeus_url() + ConstantsAmadeusDroid.HOST_AMADEUS_WEB +
		"/" + config.getUser_login() + "/" + config.getUser_password();
		RESTFileContent rfc = new RESTFileContent();
		try {
			
			InputStream is = rfc.getRESTFileContent(url);
			String courses = rfc.toString(is);
			
			rfc = new RESTFileContent();
			String url2 = url.replace("service", "countcourses");
			InputStream is2 = rfc.getRESTFileContent(url2);
			String count = rfc.toString(is2);
			int countCourses = JSONReader.countCourses(count);
			
			ArrayList<Course> listCourses = JSONReader.getListCourses(courses, countCourses);
			ArrayList<CourseUser> listCourseUser = JSONReader.getListCourseUser(courses);
			
			FacadeAmadeusDroid.getInstance(this).syncronizeAmadeus(listCourses, listCourseUser, null, null);
			
			config = FacadeAmadeusDroid.getInstance(this).getConfig();
			if (config.getVersionSync() == 1) {
				createNotification();
			}
			
							
		} catch (ClientProtocolException e) {
			Log.e(CATEGORIA, e.getMessage(), e);
		} catch (IOException e) {
			Log.e(CATEGORIA, e.getMessage(), e);
		} catch (JSONException e) {
			Log.e(CATEGORIA, e.getMessage(), e);
		} catch (ParseException e) {
			Log.e(CATEGORIA, e.getMessage(), e);
		}
		stopSelf();
	}
	
	private void createNotification() {
		
		String mensagemBarraStatus = ConstantsAmadeusDroid.TICKER_TEXT_NOTIFICATION;

		CharSequence titulo = ConstantsAmadeusDroid.TITLE_NOTIFICATION;

		String mensagem = ConstantsAmadeusDroid.MESSAGE_NOTIFICATION;
		
		NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
		
		Intent it = new Intent("HOME");
						
		PendingIntent p = PendingIntent.getActivity(this, 0, it, 0);
		
		Notification n = new Notification(R.drawable.amadeus_notification, mensagemBarraStatus, System.currentTimeMillis());
		
		n.setLatestEventInfo(this, titulo, mensagem, p);
		
		n.vibrate = new long[] { 100, 250, 100, 500 };
		
		nm.notify(R.string.app_name, n);
		
	}
	

}
